package com.icim.cxbh.export.parser;

import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.icim.cxbh.export.exception.XmlParseException;
import com.icim.cxbh.export.pojo.BoxInfo;
import com.icim.cxbh.export.pojo.EaInfo;
import com.icim.cxbh.export.pojo.OqcInfo;

public final class XmlParser {

    private static final String XML_ROOT = "transaction";

    @SuppressWarnings("unchecked")
    public static final OqcInfo parse(String xml) throws DocumentException, XmlParseException, ParseException {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new StringReader(xml));
        Element root = document.getRootElement();
        if (root == null) {
            throw new XmlParseException("Invalid XML format");
        }
        if (!XML_ROOT.equals(root.getName())) {
            throw new XmlParseException("Root element is not " + XML_ROOT);
        }
        
        List<Element> iaryElements = root.elements("iary");
        List<BoxInfo> boxInfoList = new ArrayList<BoxInfo>();
        for (int i = 0; i < iaryElements.size(); i++) {
        	Element boxIdElement = iaryElements.get(i).element("box_id");
            if (boxIdElement == null) {
                throw new XmlParseException("No box_id element");
            }
            Element modelIdElement = iaryElements.get(i).element("model_id");
            if (modelIdElement == null) {
                throw new XmlParseException("No model_id element");
            }
            Element workQtyElement = iaryElements.get(i).element("work_qty");
            if (workQtyElement == null) {
                throw new XmlParseException("No work_qty element");
            }
            Element stkinDateElement = iaryElements.get(i).element("stkin_date");
            if (stkinDateElement == null) {
                throw new XmlParseException("No stkin_date element");
            }
            
            List<Element> iaryAElements = iaryElements.get(i).elements("iaryA");
            List<EaInfo> eaInfos = new ArrayList<EaInfo>();
            for (Element iaryAElem : iaryAElements) {
            	
            	Element eaIdElement = iaryAElem.element("ea_id");
                if (eaIdElement == null) {
                    throw new XmlParseException("No ea_id element");
                }
                EaInfo eaInfo = new EaInfo();
                eaInfo.setEaId(eaIdElement.getTextTrim());
                eaInfos.add(eaInfo);
            }
            
            BoxInfo boxInfo = new BoxInfo();
            boxInfo.setBoxId(boxIdElement.getTextTrim());
            boxInfo.setModel(modelIdElement.getTextTrim());
            boxInfo.setWorkQty(Integer.parseInt(workQtyElement.getTextTrim()));
            boxInfo.setStkinDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(stkinDateElement.getTextTrim()));
            boxInfo.setEaInfos(eaInfos);
            boxInfoList.add(boxInfo);
        }
        
        OqcInfo oqcInfo = new OqcInfo();
        oqcInfo.setBox_cnt(boxInfoList.size());
        oqcInfo.setBoxInfoList(boxInfoList);
      
        return oqcInfo;
    }

}
